<?php
namespace app\website\model;

use app\BaseModel;
use think\facade\Db;

class StarCate extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_web_star_cate';


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 10:51
     * @功能说明:列表
     */
    public function cateList($dis){

        $data = $this->where($dis)->order('top desc,id desc')->select()->toArray();

        $data = $this->getTree($data,0);

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-09 17:05
     * @功能说明:配置详情
     */
    public function cateInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)? $data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:配置添加
     */
    public function cateAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:配置编辑
     */
    public function cateUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }




    /**
     * @param $data
     * @param $pId
     * @return array
     * 递归无限极
     */
    public function getTree($data, $pId){
        $tree = array();
        if(!empty($data)){
            foreach($data as $k => $v) {
                if($v['pid'] == $pId) {
                    $v['children'] = $this->getTree($data, $v['id']);
                    if(empty($v['children'])){
                        unset($v['children']);
                    }
                    $tree[]   = $v;
                }
            }
        }
        return $tree;
    }


    /**
     * @param $id
     * 回显
     */
    public function reset($id,&$data=array()){
        $cate   = Db::name($this->name)->where(['id'=>$id])->find();
        if(!empty($cate)&&$cate['pid']!=$cate['id']){
            array_unshift($data,$cate['id']);
            $this->reset($cate['pid'],$data);
        }
        return $data;
    }











}